00001 // Emacs Mode Line: -*- Mode:c++;-*- 00002 // ------------------------------------------------------------- 00003 /* 00004 * Copyright (c) 2013 Battelle Memorial Institute 00005 * Licensed under modified BSD License. A copy of this license can be found 00006 * in the LICENSE file in the top level directory of this distribution. 00007 */ 00008 // ------------------------------------------------------------- 00009 /** 00010 * @file newton_raphson_solver.hpp 00011 * @author William A. Perkins 00012 * @date 2015-03-26 11:45:34 d3g096 00013 * 00014 * @brief 00015 * 00016 * 00017 */ 00018 // ------------------------------------------------------------- 00019 00020 #ifndef _newton_raphson_solver_hpp_ 00021 #define _newton_raphson_solver_hpp_ 00022 00023 #include <gridpack/math/nonlinear_solver.hpp> 00024 #include <gridpack/math/newton_raphson_solver_implementation.hpp> 00025 00026 namespace gridpack { 00027 namespace math { 00028 00029 // ------------------------------------------------------------- 00030 // class NewtonRaphsonSolver 00031 // ------------------------------------------------------------- 00032 /// Solve a system of nonlinear equations using the Newton-Raphson method in parallel 00033 /** 00034 * This (fully functional) class is intended as an example of how to 00035 * implement a nonlinear solver. It has no advantage over the general 00036 * NonlinearSolver class, which relies on the underlying math library. 00037 * It is, however, a drop-in replacement for NonlinearSolver. 00038 * 00039 * Users of this class must specify functions or functors that build 00040 * the \ref JacobianBuilder "Jacobian" Matrix and the \ref 00041 * FunctionBuilder "right hand side" Vector. Typically, it's best to 00042 * use functor classes or structs, since extra required information 00043 * can be available to the Matrix/Vector construction. 00044 * 00045 * This class is simply an interface to the 00046 * NewtonRaphsonSolverImplementation class. 00047 */ 00048 template <typename T, typename I = int> 00049 class NewtonRaphsonSolverT 00050 : public NonlinearSolverT<T, I> { 00051 public: 00052 00053 typedef typename NonlinearSolverImplementation<T, I>::VectorType VectorType; 00054 typedef typename NonlinearSolverImplementation<T, I>::MatrixType MatrixType; 00055 typedef typename NonlinearSolverImplementation<T, I>::JacobianBuilder JacobianBuilder; 00056 typedef typename NonlinearSolverImplementation<T, I>::FunctionBuilder FunctionBuilder; 00057 00058 /// Default constructor. 00059 /** 00060 * @e Collective. 00061 * 00062 * A NonlinearSolver must be constructed simultaneously on all 00063 * processes involved in @c comm. 00064 * 00065 * @param comm communicator on which the instance is to exist 00066 * @param local_size number Jacobian rows and Vector entries to be owned by this process 00067 * @param form_jacobian function to fill the Jacobian Matrix, \f$\left[ \mathbf{J}\left( \mathbf{x} \right) \right]\f$ 00068 * @param form_function function to fill the RHS function Vector, \f$\mathbf{F}\left( \mathbf{x} \right)\f$ 00069 * 00070 * @return new NonlinearSolver instance 00071 */ 00072 NewtonRaphsonSolverT(const parallel::Communicator& comm, 00073 const int& local_size, 00074 JacobianBuilder form_jacobian, 00075 FunctionBuilder form_function) 00076 : NonlinearSolverT<T, I>() 00077 { 00078 this->p_setImpl(new NewtonRaphsonSolverImplementation<T, I>(comm, local_size, 00079 form_jacobian, 00080 form_function)); 00081 } 00082 00083 /// Construct with an existing Jacobian Matrix 00084 NewtonRaphsonSolverT(MatrixType& J, 00085 JacobianBuilder form_jacobian, 00086 FunctionBuilder form_function) 00087 : NonlinearSolverT<T, I>() 00088 { 00089 this->p_setImpl(new NewtonRaphsonSolverImplementation<T, I>(J, 00090 form_jacobian, 00091 form_function)); 00092 } 00093 00094 /// Destructor 00095 /** 00096 * This must be called simultaneously by all processes involved in 00097 * the \ref parallel::Communicator "communicator" used for \ref 00098 * NewtonRaphsonSolver() "construction". 00099 */ 00100 ~NewtonRaphsonSolverT(void) 00101 { } 00102 }; 00103 00104 typedef NewtonRaphsonSolverT<ComplexType> ComplexNewtonRaphsonSolver; 00105 typedef NewtonRaphsonSolverT<RealType> RealNewtonRaphsonSolver; 00106 typedef ComplexNewtonRaphsonSolver NewtonRaphsonSolver; 00107 00108 } // namespace math 00109 } // namespace gridpack 00110 00111 #endif